package com.group_e.Bubble.client;

//import java.io.IOException;
//import java.io.PrintWriter;
//import javax.servlet.http.*;

//import com.google.appengine.api.datastore.Query;
//import com.google.appengine.api.rdbms.AppEngineDriver;
import java.sql.*;

public class User_Operate_D 
{
	private Connection con;
	//private String User_ID;

	public User_Operate_D(Connection con, String User_ID) {
		
		this.con = con;
		//this.User_ID = User_ID;
	}
	
	public ResultSet addUser(User_D u) throws SQLException
	{
		StringBuilder sql = new StringBuilder();
		
		  sql.append("INSERT INTO User ( ")
		  		.append("?, ")
		        .append("?, ")
		        .append("?, ")
		        .append("?, ")
		        .append("?, ")
		        .append("? ); ");

		ResultSet rs = null;
		PreparedStatement pstmt = null;
		try {
			   pstmt = con.prepareStatement(sql.toString());
			   pstmt.setString(1, u.getUserid().toString());
			   pstmt.setString(2, u.getFname());
			   pstmt.setString(3, u.getLname());
			   pstmt.setString(4, u.getUname());
			   pstmt.setString(5, u.getPassword());
			   pstmt.setString(6, u.getEmail());
			rs = pstmt.executeQuery();
		}
		catch (SQLException e) {
	        e.printStackTrace();
	    }
		finally {
				pstmt.close();
				}
		
		return rs;
	}

	
	public ResultSet userByEmail(String em_add) throws SQLException
	{
		if (!em_add.contains("@"))
			return null;
		
		String sql = "SELECT USER_ID FROM User WHERE EMAIL_ADDRESS = "+em_add+";";
		
		ResultSet rs = null;
		Statement stmt = null;
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
		}
		catch (SQLException e) {
	        e.printStackTrace();
	    }
		finally {
			if (stmt != null) {
				stmt.close();
				}
			}
		
		return rs;	
		
	}

}
